function loadRadServices(groupLbx, serviceLbx){
    var groupId =   $('#' + groupLbx).val();
    var ticketId =   $('#ticketId').val();
    var emptyOption =   '<option value="0">- - ' + _('Service') + ' - -</option>';
    if (groupId==0) {
        $('#' + serviceLbx).html(emptyOption);
    }
    else {
        $.ajax({
            type:    "GET",
            url:      "../Endoscopy/GetServicesByGroupAjax?set_ajax_view",
            data:     "groupId=" + groupId + "&ticketId=" + ticketId,
            dataType: "json",
            success:    function(data) {
                if (data.error.code == 'OK') {
                    if(data.result.length > 0){
                        $('#' + serviceLbx).html(data.result);
                    }
                    else {
                        $('#' + serviceLbx).html(emptyOption);
                    }
                }
                else {
                    displayError(data.error.mess);              
                }
            }
        });
    }
}

function addRadService(serviceLbx, serviceTbl) {       
    if ($('#' + serviceLbx).val() > 0) {
        // Locale
        lcInt = locale(decimalPoint, thousandSep, 0);
        // Values for new row
        serviceId   =   $('#' + serviceLbx).val();
        serviceName =   $('#' + serviceLbx + ' option:selected').attr('label');
        price       =   $('#' + serviceLbx + ' option:selected').attr('price');
        insPrice    =   $('#' + serviceLbx + ' option:selected').attr('insprice');
        patPrice    =   $('#' + serviceLbx + ' option:selected').attr('patprice');
        // Check service duplicated
        if(!checkServiceDuplicted(serviceId)){
            return;
        }
        // Get the current max index
        if($('#' + serviceTbl + ' tbody tr').length == 1){
            index   =   1;
        }
        else {
            obj     =   $('#' + serviceTbl + ' tbody tr:first');
            index   =   obj.attr('id');
            index   =   parseInt(index) + 1;
        }
        newRow      =   '<tr id=\"' + index + '\">' +
            '<td class="first"><input type="hidden" name="services[]" value=\"' + serviceId + '\"/>' +
                '<span>' + serviceName + '</span></td>'+
            '<td><span id=\"price_' + index + '\" value=\"' + price +  '\">' + toLcString(price, lcInt)  + '</span></td>' +
            '<td><span id=\"insPrice_' + index + '\" value=\"' + insPrice +  '\">' + toLcString(insPrice, lcInt) + '</span></td>' +
            '<td><span id=\"patPrice_' + index + '\" value=\"' + patPrice +  '\">' + toLcString(patPrice, lcInt) + '</span></td>' +
            '<td class="last action"><a href="javascript:remove\(\'' + serviceTbl + '\',' + index + '\);" title=\"' +
                _("Delete") + '\"><img src="../images/remove_small.png"/></a></td>' +
            '</tr>';
        $('#' + serviceTbl + ' tbody').prepend(newRow);
        updateTotal(serviceTbl);
    }
}

function remove(serviceTbl, rowId) {
    $('#' + serviceTbl + ' tr\[id=\"' + rowId + '\"\]').remove();
    updateTotal(serviceTbl);
}

function updateTotal(serviceTbl){
    var totalPrice      =   0;
    var totalInsPrice   =   0;
    var totalPatPrice   =   0;
    var lcInt = locale(decimalPoint, thousandSep, 0);
    $('#' + serviceTbl + ' tbody tr').each(function(){
        if($(this).attr('id') != 'total') {
            var index   =   $(this).attr('id');
            totalPrice      +=  parseLcNum($('#price_' + index).text(), lcInt);
            totalInsPrice   +=  parseLcNum($('#insPrice_' + index).text(), lcInt);
            totalPatPrice   +=  parseLcNum($('#patPrice_' + index).text(), lcInt);
        }
    });
    $('span#totalPrice').text(toLcString(totalPrice, lcInt));
    $('span#totalInsPrice').text(toLcString(totalInsPrice, lcInt));
    $('span#totalPatPrice').text(toLcString(totalPatPrice, lcInt));
}

function checkServiceDuplicted(serviceId){
    res =   true;
    $('input[name=services\[\]]').each(function(){
        if($(this).val()==serviceId){
            res = false;
        }
    });
    return res;
}

function checkServices(){
    if($('input[name=services\[\]]').length > 0) {
        return true;
    }
    
    return false;
}

function createRadiologyTicket(ticketId, objDialog, triggeredElement, print){
    if (!isReadyToSubmit()) {
        return;
    }
    showAjaxLoader('createAssignmentLoaderDiv', 'dummy', true);
    data   =   $('#radAssignmentFrm').formSerialize();
    $.ajax({
        type:       "POST",
        url:        "../Endoscopy/SaveAssignmentTicketAjax?set_ajax_view",
        data:       data,     
        dataType:   "json",
        beforeSend: function() {
            
        },
        success:    function(data) {
             if (data.error.code == 'OK') {
                objDialog.dialog('close');
                $('#' + triggeredElement).trigger('click');
            } else {
                displayError(data.error.mess);              
            }
        }
    });
    
}

function isReadyToSubmit(){
    if (!validateSelectBox('doctorId', _('Not yet selected doctor'), 'radAssignmentMsg')) {
        return false;
    }
    
    if (!checkFullDate($('#created_yyyy'), $('#created_mm'), $('#created_dd'), true, _('Invalid date'), 'radAssignmentMsg', false)) {
        return false;
    }
    
    if (!checkHour($('#createdTime'), true, _('Invalid time'), 'radAssignmentMsg')) {
        return false;
    }
    
    if (!checkServices()) {
        $('#radAssignmentMsg').text(_('There is no any service'));
        return false;
    }
    $('#radAssignmentMsg').text('');
    
    return true;
}